#!/usr/bin/php -q
<?php
	# this must be run as a root cronjob
	require_once('/var/www/mobloggin/include/lib/S3.php');
	
	function create_www_backup($dir2backup, $file, $excludes = "")
	{
		exec("tar cvpzf $file $excludes $dir2backup", $output, $status);
		return $status;
	}
	
	function create_database_backup($db, $db_user, $db_pass, $db_file)
	{
		exec("mysqldump $db -u$db_user -p$db_pass > $db_file", $output, $status);
		return true;
	}

	function upload_and_delete_files($aws_public, $aws_private, $bucket, 
		$www_file, $db_file, $www_uri, $db_uri, $www_uri2delete, $db_uri2delete)
	{
		$s3 = new S3($aws_public, $aws_private);

		if(!$s3->putObjectFile($www_file, $bucket, $www_uri))
			s3_failure('www', $www_file, $bucket, $www_uri);

		if(!$s3->putObjectFile($db_file, $bucket, $db_uri))
			s3_failure('db', $db_file, $bucket, $db_uri);

		if(!$s3->deleteObject($bucket, $www_uri2delete))
			s3_failure('delete_www', null, $bucket, $www_uri2delete);
		
		if(!$s3->deleteObject($bucket, $db_uri2delete))
			s3_failure('delete_db', null, $bucket, $db_uri2delete);
		
	}
	
	function s3_failure($type, $file, $bucket, $uri)
	{
		# fail. notify via sms and email
		switch($type)
		{
			case 'www':
				$sub = "Moblogg.in / Sushipop Archive Backup Upload Failure";
				break;
			case 'db':
				$sub = "Moblogg.in / Sushipop Database Backup Upload Failure";
				break;
			case 'www_delete':
				$sub = "Moblogg.in / Sushipop Old Archive Backup Deletion Failure";
				break;
			case 'db_delete':
				$sub = "Moblogg.in / Sushipop Old Database Backup Deletion Failure";
				break;
			default:
				$sub = "Moblogg.in / Sushipop Unknown S3 Failure";
		}

		$msg = "Failed to upload file $file to bucket $bucket with uri $uri.";
		mail("8563710204@vtext.com", $sub, "");
		mail("davidivins@gmail.com", $sub, $msg);
		exit(1);
	}
	
	function www_failure()
	{
		# fail. notify via sms and email
		$sub = "Moblogg.in / Sushipop Archive Backup Failure";
		$msg = "Failed to build tgz archive.";
		mail("8563710204@vtext.com", $sub, "");
		mail("davidivins@gmail.com", $sub, $msg);
		exit(1);
	}

	function db_failure()
	{
		# fail. notify via sms and email
		$sub = "Moblogg.in / Sushipop Database Backup Failure";
		$msg = "Failed to dump mysql database.";
		mail("8563710204@vtext.com", $sub, "");
		mail("davidivins@gmail.com", $sub, $msg);
		exit(1);
	}

	function main()
	{
		$current_week = date('W');
		$week2delete = $current_week - 5;

		$db = "mobilesushi";
		$db_user = exec('getdblogin user');
		$db_pass = exec('getdblogin pass');
		$db_file = "/var/www/mobloggin/tmp/db_backup.dump";
		$db_uri = "db_backup_{$current_week}.dump";
		$db_uri2delete = "db_backup_{$week2delete}.dump";

		$dir2backup = "/var/www";
		$www_file = "/var/www/mobloggin/tmp/www_backup.tgz";
		$www_uri = "www_backup_{$current_week}.tgz";
		$www_uri2delete = "www_backup_{$week2delete}.tgz";
		$www_excludes = "--exclude=$www_file "
			. "--exclude=$db_file "
			. "--exclude=/var/www/davidivins/phpmyadmin "
			. "--exclude=/var/www/modavid "
			. "--exclude=/var/www/jobloggin";
		
		$aws_public = exec('getawskey public');
		$aws_private = exec('getawskey private');
		$bucket = 'mobloggin-backups';

		# create temporary backup files
		$www_status = create_www_backup($dir2backup, $www_file, $www_excludes);
		$db_status = create_database_backup($db, $db_user, $db_pass, $db_file);
		
		# check for errors, then upload files to S3
		if ($www_status == 0 && $db_status == true)
		{	
			upload_and_delete_files($aws_public, $aws_private, $bucket, 
				$www_file, $db_file, $www_uri, $db_uri, $www_uri2delete, $db_uri2delete);
		}
		else
		{
			# email and sms admin on failure
			($www_status) ? www_failure() : db_failure();
		}

		return true;
	}
	
	main();
	exit();
?>
